//
//  TTHomeHeaderView.m
//  TaoTuo
//
//  Created by admin on 2019/8/17.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTHomeHeaderView.h"
#import "TYCyclePagerView.h"
#import "TYPageControl.h"
#import "TYCyclePagerViewCell.h"
#import "TTHomeModel.h"
#import "TTMarketModel.h"
#import "TTHomeMarketViewCell.h"
#import <POP.h>
#import "TTCouponViewController.h"
#import "LLVerticalScrollADView.h"
#import "TTScrollADView.h"
#import <UIButton+WebCache.h>
#import "TTHomeHeaderCategoryView.h"
#import "TTHomeActivityScrollView.h"

static NSString *pagerCellIdentifier = @"pagerCellIdentifier";
static NSString *cellID = @"home_market_cell_id";

@interface TTHomeHeaderView ()<TYCyclePagerViewDataSource, TYCyclePagerViewDelegate, UITableViewDelegate, UITableViewDataSource, LLVerticalScrollADViewDelegate>
{
    CGFloat allViewH;
    CGFloat bgViewH;
    CGFloat leftSpace;
    CGFloat space;

    TTWebCastDetailModel *liveModel;
    TTWebCastDetailModel *dingzhiModel;
    TTWebCastDetailModel *designModel;
}

@property (nonatomic, strong) TTHomeModel *homeModel;
@property (nonatomic, strong) NSArray<TTMarketModel *> *marketArray;

@property (nonatomic, strong) TYCyclePagerView *pagerView;
@property (nonatomic, strong) TYPageControl *pageControl;

@property (nonatomic, strong) NSArray *datasArray;      // banner

// 圆角背景view
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIStackView *topStackView;
@property (nonatomic, strong) LLVerticalScrollADView *adView;
/// 广告1
@property (nonatomic, strong) UIImageView *ad1ImageView;
/// 分类view
@property (nonatomic, strong) TTHomeHeaderCategoryView *categoryView;
/// 广告3
@property (nonatomic, strong) UIImageView *ad3ImageView;

//@property (nonatomic, strong) UILabel *tuoLabel;    // 选托
@property (nonatomic, strong) UIButton *tuoBtn; // 选托
@property (nonatomic, strong) UIView *rightBigView; // 女戒
@property (nonatomic, strong) UIImageView *nvjie_imgView;
@property (nonatomic, strong) UILabel *nvjieLabel;
@property (nonatomic, strong) UILabel *nvjie_countLabel;
@property (nonatomic, strong) UIButton *nvjie_shopBtn;


@property (nonatomic, strong) UIView *leftTopView;  // 套链
@property (nonatomic, strong) UIImageView *diaozhui_imgView;
@property (nonatomic, strong) UILabel *diaozhuiLabel;
@property (nonatomic, strong) UILabel *diaozhui_countLabel;
@property (nonatomic, strong) UIButton *diaozhui_shopBtn;


//@property (nonatomic, strong) UIButton *allTypeBtn; // 全部款式
@property (nonatomic, strong) UIView *leftbottomView;  // 对戒
@property (nonatomic, strong) UIImageView *dj_imgView;
@property (nonatomic, strong) UILabel *djLabel;
@property (nonatomic, strong) UILabel *dj_countLabel;
@property (nonatomic, strong) UIButton *dj_shopBtn;



//@property (nonatomic, strong) UILabel *zhuanLabel;  // 选钻
@property (nonatomic, strong) UIButton *zhuanBtn; // 选托

@property (nonatomic, strong) UIView *luoshiView;   // 裸石
@property (nonatomic, strong) UIImageView *luoshi_imgView;
@property (nonatomic, strong) UILabel *luoshiLabel;
@property (nonatomic, strong) UILabel *luoshi_countLabel;
@property (nonatomic, strong) UIButton *luoshi_shopBtn;

/// 活动
@property (nonatomic, strong) TTHomeActivityScrollView *activityView;


@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *upDateTime;
@property (nonatomic, copy) dispatch_block_t clickMoreButtonBlock;

@end

@implementation TTHomeHeaderView

+ (instancetype)headerViewWithBannerArray:(NSArray *)bannerArray
{
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, homeHeaderViewH);
    return [[self alloc] initWithFrame:rect bannerArray:bannerArray];
}

- (instancetype)initWithFrame:(CGRect)frame bannerArray:(NSArray *)bannerArray
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = UIColorHex(#F5F5F5);
        
        allViewH = 48 * kScale;
        bgViewH = homeHeaderViewH - cycleViewH + 10 * kScale + 110;
        
        leftSpace = 10 * kScale;
        space = 10 * kScale;
        
        self.datasArray = bannerArray;
        
        [self setupSubviews];
        
        
    }
    return self;
}

- (CGFloat)setGoldArrayWithArray:(NSArray<TTMarketModel *> *)goldArray
{
    self.marketArray = goldArray;
    [self.tableView reloadData];
    
    [self setNeedsLayout];
    
    [self layoutIfNeeded];
    
    return self.height;

}

- (void)setHomeModel:(TTHomeModel *)homeModel marketArray:(NSArray<TTMarketModel *> *)marketArray upDateTime:(nonnull NSString *)upDateTime clickMoreButtonBlock:(nonnull dispatch_block_t)clickMoreButtonBlock
{
    self.homeModel = homeModel;
    self.marketArray = marketArray;
    self.upDateTime = upDateTime;
    self.clickMoreButtonBlock = clickMoreButtonBlock;
    
    
    self.datasArray = homeModel.banner;
    self.has = homeModel.ScrollDisplay.count > 0;
    [self.adView setupDatasArray:homeModel.ScrollDisplay];
    if (homeModel.ad1) {
        [self.ad1ImageView sd_setImageWithURL:[NSURL URLWithString:homeModel.ad1.ImageUrl] placeholderImage:nil options:SDWebImageRetryFailed];
    }
    
    if (homeModel.ad3) {
        [self.ad3ImageView sd_setImageWithURL:[NSURL URLWithString:homeModel.ad3.ImageUrl] placeholderImage:nil options:SDWebImageRetryFailed];
    }
    
    self.activityView.dataArray = self.homeModel.ad2;
    
    self.pageControl.numberOfPages = self.datasArray.count;
    [self.pagerView reloadData];
    
    NSString *str = [NSString stringWithFormat:@" / 现货%ld件", homeModel.StyleLibraryStockTotalSum];
    [self.tuoBtn setTitle:str forState:UIControlStateNormal];
    
    [self addCountAnimatinWithLabel:self.nvjie_countLabel key:@"nv_count_key" unit:@"件" toValue:homeModel.WomenRingCount];
    
    [self addCountAnimatinWithLabel:self.diaozhui_countLabel key:@"diaozhui_count_key" unit:@"件" toValue:homeModel.PendantCount];
    
    [self addCountAnimatinWithLabel:self.dj_countLabel key:@"dj_count_key" unit:@"件" toValue:homeModel.PairOfRingCount];
    
    [self addCountAnimatinWithLabel:self.luoshi_countLabel key:@"luoshi_count_key" unit:@"粒" toValue:homeModel.DiamondsCount.integerValue];
    
    [self setupTableHeaderView];
    [self.tableView reloadData];
    
    [self setNeedsLayout];
    
}

// 数字动画
- (void)addCountAnimatinWithLabel:(UILabel *)label key:(NSString *)key unit:(NSString *)unit toValue:(NSInteger)toValue
{
    POPAnimatableProperty *prop = [POPAnimatableProperty propertyWithName:key initializer:^(POPMutableAnimatableProperty *prop) {
        
        prop.writeBlock = ^(id obj, const CGFloat values[]) {
            UILabel *la = (UILabel *)obj;
            la.attributedText = [self setAttributeStringLastString:[NSString stringWithFormat:@"%.0f%@", values[0], unit]];
        };
        //力学阀值,值越大writeBlock的调用次数越少
        prop.threshold = 1;
    }];
    POPBasicAnimation *anBasic = [POPBasicAnimation linearAnimation];
    anBasic.property = prop;
    anBasic.fromValue = @(0);
    anBasic.toValue = @(toValue);
    anBasic.duration = 1;
    [label pop_addAnimation:anBasic forKey:key];
}


#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.datasArray.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    TTHomeBanner *model = self.datasArray[index];
    TYCyclePagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:pagerCellIdentifier forIndex:index];
    [cell setImageUrlString:model.ImageUrl isVideo:NO];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(pageView.width, pageView.height);
    layout.itemHorizontalCenter = YES;  // 居中显示
    layout.itemSpacing = 0;
    layout.layoutType = TYCyclePagerTransformLayoutNormal;
//    layout.minimumScale = 1;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    _pageControl.currentPage = toIndex;
    //[_pageControl setCurrentPage:newIndex animate:YES];
    //    NSLog(@"%ld ->  %ld",fromIndex,toIndex);
}

- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index
{
//    NSLog(@"选中了 -->%ld", index);
    TTHomeBanner *bannerModel = self.datasArray[index];
    [LLRouteTool skipViewControllerWIthSkipType:bannerModel.SkipType skipTarget:bannerModel.SkipTarget];
    
}

// 点击全部款式
- (void)allTypeBtnClick:(UIButton *)sender
{
    if (self.clickGoodsBlock) {
        self.clickGoodsBlock(sender.titleLabel.text);
    }
}

- (void)setupSubviews
{
    [self addPagerView];
    [self addPageControl];
    
    [self.pagerView reloadData];
    
    [self addSubviews];
}

- (void)addPagerView
{
    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc]init];
    pagerView.frame = CGRectMake(0, 0, self.width, cycleViewH);
    //    pagerView.isInfiniteLoop = NO;
    pagerView.autoScrollInterval = 3.0;
    pagerView.dataSource = self;
    pagerView.delegate = self;
    [pagerView registerClass:[TYCyclePagerViewCell class] forCellWithReuseIdentifier:pagerCellIdentifier];
    [self addSubview:pagerView];
    self.pagerView = pagerView;
}

- (void)addPageControl
{
    TYPageControl *pageControl = [[TYPageControl alloc]init];
    pageControl.frame = CGRectMake(0, self.pagerView.bottom - 50 * kScale, self.width, 20 * kScale);
    pageControl.currentPageIndicatorSize = CGSizeMake(20, 8);
    pageControl.pageIndicatorSize = CGSizeMake(8, 8);
    pageControl.currentPageIndicatorTintColor = kMainColor;
    pageControl.pageIndicatorTintColor = UIColorHex(DDDDDD);
    pageControl.hidesForSinglePage = YES;
    pageControl.numberOfPages = self.datasArray.count;
    [self.pagerView addSubview:pageControl];
    self.pageControl = pageControl;
}

- (NSMutableAttributedString *)setAttributeString:(NSString *)string
{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:string];
    [attrStr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16], NSForegroundColorAttributeName : UIColorHex(333333)} range:NSMakeRange(0, 2)];
    return attrStr;
}

- (NSMutableAttributedString *)setAttributeStringLastString:(NSString *)string
{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:string];
    [attrStr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15], NSForegroundColorAttributeName : UIColorHex(333333)} range:NSMakeRange(string.length - 1, 1)];
    return attrStr;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.marketArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTMarketModel *model = self.marketArray[indexPath.row];
    TTHomeMarketViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.model = model;
    return cell;
}

// 实时金价更多
- (void)clickMoreBtn
{
    if (self.clickMoreButtonBlock) {
        self.clickMoreButtonBlock();
    }
}


- (void)setupTableHeaderView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width - 20 * kScale, 50)];
    [LLLineUtilies addLineInView:view startPoint:CGPointMake(10, view.height) endPoint:CGPointMake(view.width - 10, view.height) lineColor:kSeparatorColor];
    
    NSString *str = [NSString stringWithFormat:@" / %@", self.upDateTime];
    UIButton *zBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"实时金价"] target:nil action:nil];
    [zBtn setTitle:str forState:UIControlStateNormal];
    zBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [zBtn setTitleColor:UIColorHex(#999999) forState:UIControlStateNormal];
    zBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [view addSubview:zBtn];
    [zBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.height.equalTo(view);
    }];
    
    UIButton *btn = [UIButton createButtonWithFrame:CGRectMake(view.width - 60, zBtn.top, 50, view.height) image:[UIImage imageNamed:@"btn_right_arrow"] title:@"更多" titleColor:UIColorHex(666666) titleFont:[UIFont systemFontOfSize:12] space:0 backgroundColor:nil target:self action:@selector(clickMoreBtn)];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [view addSubview:btn];
    
    self.tableView.tableHeaderView = view;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.bgView.frame = CGRectMake(leftSpace, 155 * kScale, self.width - 2 * space, bgViewH);
    
    self.topStackView.frame = CGRectMake(leftSpace, 5, self.bgView.width - 20, 30);
    
    CGFloat avH = self.has ? allViewH : 0;
    self.adView.hidden = !self.has;
    self.adView.frame = CGRectMake(leftSpace, self.topStackView.bottom + 5, self.bgView.width - 20, avH);
    [self.adView setNeedsLayout];
    
    self.ad1ImageView.hidden = self.homeModel.ad1 == nil;
    CGFloat ad1ImageViewH = self.homeModel.ad1 ? 92 : 0;
    CGFloat ad1Top = avH > 0 ? self.adView.bottom + 15 : self.adView.top;
    self.ad1ImageView.frame = CGRectMake(leftSpace, ad1Top, self.adView.width, ad1ImageViewH);
    
    
    CGFloat tuoLabelTop = ad1ImageViewH > 0 ? self.ad1ImageView.bottom + space + 5 : ad1Top;
    self.tuoBtn.frame = CGRectMake(leftSpace, tuoLabelTop, self.adView.width, 15 * kScale);
    CGFloat w = (self.adView.width - 10) / 2.0;
    self.rightBigView.frame = CGRectMake(leftSpace,self.tuoBtn.bottom +  space, w, 125 * kScale);
    self.nvjie_imgView.frame = CGRectMake(30, 0, self.rightBigView.width - 60, self.rightBigView.height - 20 * kScale);
    self.nvjieLabel.frame = CGRectMake(0, 50 * kScale, self.rightBigView.width, 22 * kScale);
    self.nvjie_countLabel.frame = CGRectMake(0, self.nvjie_imgView.bottom - 30 * kScale, self.rightBigView.width, 30 * kScale);
    self.nvjie_shopBtn.frame = CGRectMake(0, 105 * kScale, self.rightBigView.width - 5, 20 * kScale);
    
    
    CGFloat h = (self.rightBigView.height - 10) / 2.0;
    self.leftTopView.frame = CGRectMake(self.rightBigView.right + 10, self.rightBigView.top, w, h);
    self.diaozhui_imgView.frame = CGRectMake(7 * kScale, 5 * kScale, 35 * kScale, h - 10 * kScale);
    self.diaozhuiLabel.frame = CGRectMake(self.diaozhui_imgView.right + 5, 0, 50 * kScale, self.leftTopView.height);
    self.diaozhui_shopBtn.frame = CGRectMake(40, self.leftTopView.height - 20 * kScale, self.leftTopView.width - 45, 20 * kScale);
    self.diaozhui_countLabel.frame = CGRectMake(self.diaozhuiLabel.right, 15 * kScale, self.leftTopView.width - self.diaozhuiLabel.right, 25 * kScale);
    
//    self.allTypeBtn.frame = CGRectMake(self.leftTopView.left, self.leftTopView.bottom + 5, w, h);
    self.leftbottomView.frame = CGRectMake(self.leftTopView.left, self.leftTopView.bottom + 10, w, h);
    self.dj_imgView.frame = CGRectMake(7 * kScale, 5 * kScale, 35 * kScale, h - 10 * kScale);
    self.djLabel.frame = CGRectMake(self.dj_imgView.right + 5, 0, 50 * kScale, self.leftbottomView.height);
    self.dj_shopBtn.frame = CGRectMake(40, self.leftbottomView.height - 20 * kScale, self.leftbottomView.width - 45, 20 * kScale);
    self.dj_countLabel.frame = CGRectMake(self.djLabel.right, 15 * kScale, self.leftbottomView.width - self.djLabel.right, 25 * kScale);
    
    self.categoryView.frame = CGRectMake(0, _rightBigView.bottom + 20, _bgView.width, 106);
    
    
    self.zhuanBtn.frame = CGRectMake(leftSpace, self.categoryView.bottom + space, self.adView.width, 15 * kScale);
    
    
    self.luoshiView.frame = CGRectMake(leftSpace, self.zhuanBtn.bottom + space, self.adView.width, 80 * kScale);
    self.luoshi_imgView.frame = CGRectMake(5, 6 * kScale, 100 * kScale, self.luoshiView.height - 10 * kScale);
    self.luoshiLabel.frame = CGRectMake(self.luoshi_imgView.right - 10, 12 * kScale, 40 * kScale, 25 * kScale);
    self.luoshi_shopBtn.frame = CGRectMake(self.luoshiView.right - 85 * kScale, self.luoshiView.height - 20 * kScale, 70 * kScale, 20 * kScale);
    self.luoshi_countLabel.frame = CGRectMake(self.luoshiLabel.right, 27 * kScale, self.luoshiView.width - self.luoshiLabel.right - self.luoshi_shopBtn.width + 20, 25 * kScale);
    
    [self setBtnTitleAndImageSpace:0 btn:_nvjie_shopBtn];
    [self setBtnTitleAndImageSpace:0 btn:_diaozhui_shopBtn];
    [self setBtnTitleAndImageSpace:0 btn:_luoshi_shopBtn];
    [self setBtnTitleAndImageSpace:0 btn:_dj_shopBtn];
//    [self setBtnTitleAndImageSpace:5 btn:_allTypeBtn];
    
    self.bgView.height = self.luoshiView.bottom + 10;
    
    self.activityView.hidden = self.homeModel.ad2.count == 0;
    CGFloat activityH = self.homeModel.ad2.count > 0 ? 120 : 0;
    self.activityView.frame = CGRectMake(0, self.bgView.bottom + 15, self.width, activityH);
    
    
    CGFloat tableViewHeaderH = self.marketArray.count == 0 ? 0 : 50;
    CGFloat tableBGViewH = self.marketArray.count * HomeTableViewRowH + tableViewHeaderH;
    CGFloat tableTop = activityH > 0 ? self.activityView.bottom + 15 : self.activityView.top;
    self.tableView.frame = CGRectMake(_bgView.left, tableTop, _bgView.width, tableBGViewH);
    
    
    self.ad3ImageView.hidden = self.homeModel.ad3 == nil;
    CGFloat ad3H = self.homeModel.ad3 ? 92 : 0;
    self.ad3ImageView.frame = CGRectMake(_bgView.left, self.tableView.bottom + 15, _bgView.width, ad3H);
    
    
    self.height = ad3H > 0 ? self.ad3ImageView.bottom + 5 : (self.marketArray.count > 0 ? self.tableView.bottom + 5 : self.tableView.top - 10);
    
    
}

- (void)setBtnTitleAndImageSpace:(CGFloat)space btn:(UIButton *)btn
{
    CGFloat s = space * 0.5;
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width - s , 0, btn.imageView.frame.size.width + s);
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width + s, 0, -btn.titleLabel.frame.size.width - s);
}

- (UIView<LLVerticalScrollADViewDataSource> *)createADViewWithVerticalScrollADView:(LLVerticalScrollADView *)scrollADView
{
    TTScrollADView *adView = [[TTScrollADView alloc] initWithFrame:scrollADView.bounds];
    return adView;
}

- (void)addSubviews
{
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.cornerRadius = 10;
    [self addSubview:_bgView];
    
    UIButton *oneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [oneBtn setImage:[UIImage imageNamed:@"homezuan"] forState:UIControlStateNormal];
    [oneBtn setTitle:@" 南非甄钻" forState:UIControlStateNormal];
    [oneBtn setTitleColor:UIColorHex(666666) forState:UIControlStateNormal];
    oneBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    UIButton *twoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [twoBtn setImage:[UIImage imageNamed:@"homejin"] forState:UIControlStateNormal];
    [twoBtn setTitle:@" 金工石结算" forState:UIControlStateNormal];
    [twoBtn setTitleColor:UIColorHex(666666) forState:UIControlStateNormal];
    twoBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    UIButton *threeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [threeBtn setImage:[UIImage imageNamed:@"homepin"] forState:UIControlStateNormal];
    [threeBtn setTitle:@" 国际钻石交易中心旗下品牌" forState:UIControlStateNormal];
    [threeBtn setTitleColor:UIColorHex(666666) forState:UIControlStateNormal];
    threeBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    
    UIStackView *topStackView = [[UIStackView alloc] init];
    topStackView.axis = UILayoutConstraintAxisHorizontal;
    topStackView.alignment = UIStackViewAlignmentCenter;
    topStackView.distribution = UIStackViewDistributionEqualSpacing;
    topStackView.spacing = 5;
    [topStackView addArrangedSubview:oneBtn];
    [topStackView addArrangedSubview:twoBtn];
    [topStackView addArrangedSubview:threeBtn];
    [_bgView addSubview:topStackView];
    self.topStackView = topStackView;
    
    _adView = [[LLVerticalScrollADView alloc] initWithFrame:CGRectMake(0, 0, self.width - 2 * space - 20, allViewH) datasArray:self.homeModel.ScrollDisplay delegate:self];
    [_bgView addSubview:_adView];
    
    
    
    _ad1ImageView = [[UIImageView alloc] init];
    _ad1ImageView.userInteractionEnabled = YES;
    _ad1ImageView.contentMode = UIViewContentModeScaleAspectFill;
    [_bgView addSubview:_ad1ImageView];
    UITapGestureRecognizer *adTap1 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        TTHomeADModel *m = self.homeModel.ad1;
        [LLRouteTool skipViewControllerWIthSkipType:m.SkipType skipTarget:m.SkipTarget];
    }];
    [_ad1ImageView addGestureRecognizer:adTap1];
    
    _tuoBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"选托"] target:nil action:nil];
    _tuoBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_tuoBtn setTitleColor:UIColorHex(#999999) forState:UIControlStateNormal];
    _tuoBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_bgView addSubview:_tuoBtn];
    
    
    _rightBigView = [[UIView alloc] init];
    _rightBigView.backgroundColor = UIColorHex(F4E3FF);
    _rightBigView.layer.cornerRadius = 5;
    [_bgView addSubview:_rightBigView];
    @weakify(self)
    UITapGestureRecognizer *nv_tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self)
        if (self.clickGoodsBlock) {
            self.clickGoodsBlock(@"女戒");
        }
    }];
    [_rightBigView addGestureRecognizer:nv_tap];
    
    _nvjie_imgView = [[UIImageView alloc] init];
    _nvjie_imgView.image = [UIImage imageNamed:@"home_nvjie"];
    _nvjie_imgView.contentMode = UIViewContentModeScaleAspectFit;
    [_rightBigView addSubview:_nvjie_imgView];
    
    _nvjieLabel = [UILabel labelWithTitle:@"女戒" color:UIColorHex(494949) fontSize:18];
    _nvjieLabel.textAlignment = NSTextAlignmentCenter;
    [_rightBigView addSubview:_nvjieLabel];
    
    _nvjie_countLabel = [UILabel labelWithTitle:@"0件" color:kMainColor fontSize:24];
    _nvjie_countLabel.textAlignment = NSTextAlignmentCenter;
    [_rightBigView addSubview:_nvjie_countLabel];

    _nvjie_shopBtn = [UIButton createButtonWithFrame:CGRectZero image:[UIImage imageNamed:@"btn_right_arrow"] title:@"立即选购" titleColor:UIColorHex(999999) titleFont:[UIFont systemFontOfSize:11] space:2 backgroundColor:nil target:nil action:nil];
    [_nvjie_shopBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_rightBigView addSubview:_nvjie_shopBtn];
    
    
    _leftTopView = [[UIView alloc] init];
    _leftTopView.backgroundColor = UIColorHex(D6F2FF);
    _leftTopView.layer.cornerRadius = 5;
    [_bgView addSubview:_leftTopView];
    
    UITapGestureRecognizer *dz_tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self)
        if (self.clickGoodsBlock) {
            self.clickGoodsBlock(@"套链");
        }
    }];
    [_leftTopView addGestureRecognizer:dz_tap];
    
    _diaozhui_imgView = [[UIImageView alloc] init];
    _diaozhui_imgView.image = [UIImage imageNamed:@"category_taolian"];
    _diaozhui_imgView.contentMode = UIViewContentModeScaleAspectFit;
    [_leftTopView addSubview:_diaozhui_imgView];
    
    _diaozhuiLabel = [UILabel labelWithTitle:@"套链" color:UIColorHex(494949) fontSize:16];
    [_leftTopView addSubview:_diaozhuiLabel];
    
    _diaozhui_countLabel = [UILabel labelWithTitle:@"0件" color:kMainColor fontSize:20];
    _diaozhui_countLabel.adjustsFontSizeToFitWidth = YES;
    [_leftTopView addSubview:_diaozhui_countLabel];
    
    _diaozhui_shopBtn = [UIButton createButtonWithFrame:CGRectZero image:[UIImage imageNamed:@"btn_right_arrow"] title:@"立即选购" titleColor:UIColorHex(999999) titleFont:[UIFont systemFontOfSize:11] space:2 backgroundColor:nil target:nil action:nil];
    [_diaozhui_shopBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_leftTopView addSubview:_diaozhui_shopBtn];
    
    
    _leftbottomView = [[UIView alloc] init];
    _leftbottomView.backgroundColor = UIColorHex(DCE3FF);
    _leftbottomView.layer.cornerRadius = 5;
    [_bgView addSubview:_leftbottomView];
    
    UITapGestureRecognizer *dj_tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self)
        if (self.clickGoodsBlock) {
            self.clickGoodsBlock(@"对戒");
        }
    }];
    [_leftbottomView addGestureRecognizer:dj_tap];
    
    _dj_imgView = [[UIImageView alloc] init];
    _dj_imgView.image = [UIImage imageNamed:@"对戒_big"];
    _dj_imgView.contentMode = UIViewContentModeScaleAspectFit;
    [_leftbottomView addSubview:_dj_imgView];
    
    _djLabel = [UILabel labelWithTitle:@"对戒" color:UIColorHex(494949) fontSize:16];
    [_leftbottomView addSubview:_djLabel];
    
    _dj_countLabel = [UILabel labelWithTitle:@"0件" color:kMainColor fontSize:20];
    _dj_countLabel.adjustsFontSizeToFitWidth = YES;
    [_leftbottomView addSubview:_dj_countLabel];
    
    _dj_shopBtn = [UIButton createButtonWithFrame:CGRectZero image:[UIImage imageNamed:@"btn_right_arrow"] title:@"立即选购" titleColor:UIColorHex(999999) titleFont:[UIFont systemFontOfSize:11] space:2 backgroundColor:nil target:nil action:nil];
    [_dj_shopBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_leftbottomView addSubview:_dj_shopBtn];
    
    
    _categoryView = [[TTHomeHeaderCategoryView alloc] initWithFrame:CGRectMake(0, _rightBigView.bottom + 20, self.width - 2 * space, 106)];
    _categoryView.datasArray = @[@"男戒", @"吊坠", @"手链", @"手镯", @"耳钉", @"耳线", @"耳坠", @"女戒", @"套链", @"对戒"];
    [_bgView addSubview:_categoryView];
    
    
    _zhuanBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"选钻"] target:nil action:nil];
    [_zhuanBtn setTitle:@" / 全球钻石" forState:UIControlStateNormal];
    _zhuanBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_zhuanBtn setTitleColor:UIColorHex(#999999) forState:UIControlStateNormal];
    _zhuanBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_bgView addSubview:_zhuanBtn];
    
    
    _luoshiView = [[UIView alloc] init];
    _luoshiView.backgroundColor = UIColorHex(EBF8FE);
    _luoshiView.layer.cornerRadius = 5;
    [_bgView addSubview:_luoshiView];
    
    UITapGestureRecognizer *ls_tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self)
        if (self.clickGoodsBlock) {
            self.clickGoodsBlock(@"裸石");
        }
    }];
    [_luoshiView addGestureRecognizer:ls_tap];
    
    
    _luoshi_imgView = [[UIImageView alloc] init];
    _luoshi_imgView.image = [UIImage imageNamed:@"home_zhuanshi"];
    _luoshi_imgView.contentMode = UIViewContentModeScaleAspectFit;
    [_luoshiView addSubview:_luoshi_imgView];
    
    _luoshiLabel = [UILabel labelWithTitle:@"钻石" color:UIColorHex(494949) fontSize:18];
    [_luoshiView addSubview:_luoshiLabel];
    
    _luoshi_countLabel = [UILabel labelWithTitle:@"0粒" color:kMainColor fontSize:24];
    _luoshi_countLabel.textAlignment = NSTextAlignmentRight;
    [_luoshiView addSubview:_luoshi_countLabel];
    
    // btn_right_arrow
    _luoshi_shopBtn = [UIButton createButtonWithFrame:CGRectZero image:[UIImage imageNamed:@"btn_right_arrow"] title:@"立即选购" titleColor:UIColorHex(999999) titleFont:[UIFont systemFontOfSize:11] space:2 backgroundColor:nil target:nil action:nil];
    [_luoshi_shopBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_luoshiView addSubview:_luoshi_shopBtn];
    
    /// 活动
    _activityView = [[TTHomeActivityScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, 120)];
    _activityView.dataArray = self.homeModel.ad2;
    [self addSubview:_activityView];
    
    
    _ad3ImageView = [[UIImageView alloc] init];
    _ad3ImageView.contentMode = UIViewContentModeScaleAspectFill;
    _ad3ImageView.layer.cornerRadius = 10;
    _ad3ImageView.layer.masksToBounds = YES;
    _ad3ImageView.userInteractionEnabled = YES;
    [self addSubview:_ad3ImageView];
    UITapGestureRecognizer *adTap3 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        TTHomeADModel *m = self.homeModel.ad3;
        [LLRouteTool skipViewControllerWIthSkipType:m.SkipType skipTarget:m.SkipTarget];
    }];
    [_ad3ImageView addGestureRecognizer:adTap3];
    
}

//- (void)clickLiveBtn
//{
//    [LLRouteTool skipViewControllerWIthSkipType:@"18000" skipTarget:liveModel.LocationUrl];
//}
//
//- (void)clickDingzhiBtn
//{
//    [LLRouteTool skipViewControllerWIthSkipType:@"11500" skipTarget:dingzhiModel.LocationUrl];
//}
//
//- (void)clickDesignBtn
//{
//    [LLRouteTool skipViewControllerWIthSkipType:@"18000" skipTarget:designModel.LocationUrl];
//}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = HomeTableViewRowH;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.layer.cornerRadius = 10;
        _tableView.scrollEnabled = NO;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TTHomeMarketViewCell class]) bundle:nil] forCellReuseIdentifier:cellID];
        [self addSubview:_tableView];
    }
    return _tableView;
}


@end
